package com.ly.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ly.bean.EasybuyProduct;
import com.ly.mapper.EasybuyProductMapper;
import com.ly.service.ProductSolrService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.*;
import org.springframework.data.solr.core.query.result.HighlightEntry;
import org.springframework.data.solr.core.query.result.HighlightPage;
import org.springframework.data.solr.core.query.result.ScoredPage;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李岩
 * @date 2022/1/5-16:06
 * @describe
 */
@Component
@Service(interfaceClass = ProductSolrService.class)
public class ProductSolrServiceImpl implements ProductSolrService {
    private final Logger logger= LoggerFactory.getLogger(ProductSolrServiceImpl.class);
    @Autowired
    private EasybuyProductMapper productMapper;
    private ProductSolrServiceImpl(){
        System.out.println("进入了 ProductSolrServiceImpl  service层");
    }
    @Autowired
    private SolrTemplate solrTemplate;

    //初始化数据  把数据库中的数据添加到solrhome中
    @Override
    public void initSolrHome() throws Exception {
        List<EasybuyProduct> productList = productMapper.selectAllProduct();
        logger.info("productList  "+productList);
        solrTemplate.saveBeans("collection1",productList);

        System.out.println("saveBeans   之后");
        solrTemplate.commit("collection1");
        System.out.println("初始化数据完成");
    }
    //带关键字的高亮查询
    @Override
    public Map<String, Object> findLightHighProductByMap(Map<String, Object> map) throws Exception {
        SimpleHighlightQuery highlightQuery = new SimpleHighlightQuery();
        HighlightOptions options = new HighlightOptions();
        options.addField("prod_name");
        options.addField("prod_sellerId");
        options.setSimplePrefix("<span style='color:red;font-size:16px;'>");
        options.setSimplePostfix("</span>");
        highlightQuery.setHighlightOptions(options);
        highlightQuery.setRows(Integer.valueOf(map.get("pageSize").toString()));//每页的容量
        highlightQuery.setOffset(Long.parseLong(map.get("pageIndex").toString()));//开始的位置

        EasybuyProduct product =(EasybuyProduct) map.get("product");
        //设置高亮的查询条件
        if (product.getKeyword()!=null && !"".equals(product.getKeyword())){
            Criteria criteria =new Criteria("prod_keywords").is(product.getKeyword());

            //根据一级标题搜索
            if(product.getCategorylevel1id()!=null){
                criteria=criteria.and("prod_cate").is(product.getCategorylevel1id());
            }
            //最大值
            if (product.getMaxPrice()!=null){
                criteria=criteria.and("prod_price").lessThan(product.getMaxPrice());
            }
            //最小值
            if (product.getMinPrice()!=null){
                criteria=criteria.and("prod_price").greaterThan(product.getMinPrice());
            }

            highlightQuery.addCriteria(criteria);
        }
        //设置高亮结果
        HighlightPage<EasybuyProduct> prodPage = solrTemplate.queryForHighlightPage("collection1", highlightQuery, EasybuyProduct.class);
        List<HighlightEntry<EasybuyProduct>> prodEntryList = prodPage.getHighlighted();
        //获取高亮入口集合
        for (HighlightEntry<EasybuyProduct> prodEntry : prodEntryList) {
            EasybuyProduct prodInfo = prodEntry.getEntity();
            List<HighlightEntry.Highlight> hlList = prodEntry.getHighlights();
            //遍历每一个高亮数据
            for (HighlightEntry.Highlight hl : hlList) {
                String fieldName = hl.getField().getName();
                //判断高亮列字段
                //System.out.println("fieldName "+fieldName);
                if ("prod_name".equals(fieldName) && hl.getSnipplets().size() == 1){
                    //设定指定的高两列数据
                    prodInfo.setName( hl.getSnipplets().get(0));
                }

                if ("prod_sellerId".equals(fieldName) && hl.getSnipplets().size() == 1){
                    //设定指定的高两列数据
                    prodInfo.setSellerId( hl.getSnipplets().get(0));
                }
            }
        }

        System.out.println("productList= "+prodPage.getContent());
        Map<String, Object> resuMap = new HashMap<>();
        resuMap.put("productList",prodPage.getContent());
        resuMap.put("totalPages",prodPage.getTotalPages());
        resuMap.put("totalProduct",prodPage.getTotalElements());
        return resuMap;
    }

    @Override
    public Map<String, Object> findProductByMap(Map<String, Object> map) throws Exception {
        Query query = new SimpleQuery("*:*");
        query.setRows(Integer.valueOf(map.get("pageSize").toString()));//每页的容量
        query.setOffset(Long.parseLong(map.get("pageIndex").toString()));//开始的位置

        EasybuyProduct product =(EasybuyProduct) map.get("product");
        //执行条件
        Criteria criteria = Criteria.where("prod_stock").greaterThan(0);

        System.out.println("product  "+product);

        //根据一级标题搜索
        if(product.getCategorylevel1id()!=null){
            criteria=criteria.and("prod_cate").is(product.getCategorylevel1id());
        }
        //最大值
        if (product.getMaxPrice()!=null){
            criteria=criteria.and("prod_price").lessThan(product.getMaxPrice());
        }
        //最小值
        if (product.getMinPrice()!=null){
            criteria=criteria.and("prod_price").greaterThan(product.getMinPrice());
        }

        query.addCriteria(criteria);
        //执行
        ScoredPage<EasybuyProduct> productPage = solrTemplate.queryForPage("collection1", query, EasybuyProduct.class);

        System.out.println("productList= "+productPage.getContent());
        Map<String, Object> resuMap = new HashMap<>();
        resuMap.put("productList",productPage.getContent());
        resuMap.put("totalPages",productPage.getTotalPages());
        resuMap.put("totalProduct",productPage.getTotalElements());
        return resuMap;
    }
}
